$(function() {

// --------------------------------------------------------------------------------------------------------------------
// ---------------------------------详情页面---------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
  get_lv_reply();
  function get_lv_reply(){
    var loading = '<div class="loading">加载中...</div>';
    $(".lv-reply-list").append(loading);
    var get_lv_reply_url = jumpUrl+"/question/"+id;
    $.get(get_lv_reply_url,function(){
      }, 'json').error(function(data){
            var obj = $.parseJSON(data.responseText);
            // $.each(obj.error_messages, function(i, item){
            //   layer.msg(item[0]);
            // });
      }).success(function(data){
        var obj = data.data;
        $(".loading").remove();
        var comments = obj.comments;
        var str = ''
        if (comments.length>7) {
          $(".load-more").css("display","block")
        }else{
          $(".load-more").css("display","none")
        }
        if(comments.length > 0) {
            for (var n = 0; n < comments.length; n++) {
            str +='<li>'
              str +='<div class="lv-info">'
                str +='<div class="top-icon"></div>'
                str +='<p class="font18 colorF" style="margin:5px 0;">'+comments[n].creator_name+'</p>'
                str +='<p class="font12 colorD">'+comments[n].creator_company_name+'</p>'
                str +='<div class="oh font14" style="margin:10px 8px;">'
                  str +='<p class="fl textac colorD"><span class="colorC">'+comments[n].creator_case_count+'</span><br>接单数</p>'
                  str +='<p class="fr textac colorD"><span class="colorC">'+comments[n].creator_average_evaluate_score+'</span><br>评分</p>'
                str +='</div>'
                if(comments[n].is_following){
                str +='<a href="javascript:void(0);" class="font14 butC follow-on" data-id="'+comments[n].creator_id+'">取消关注</a>'
                }else{
               str +='<a href="javascript:void(0);" class="font14 butC follow-off" data-id="'+comments[n].creator_id+'">关注</a>'
                }
              str +='</div>'
              str +='<div class="oh">'
                str +='<div class="fl" style="margin: 10px;">'
                  str +='<a href="javascript:void(0);" class="lvimg"><img src="'+comments[n].creator_avatar+'" width="60" height="60" style="border-radius: 50%;"></a>'
                str +='</div>'
                str +='<div class="fr boxr lv-reply-info ">'
                  str +='<span class="iconleft"></span>'
                  str +='<div class="oh lv-title">'
                    str +='<a href="'+jumpUrl+'/lawyer/'+comments[n].creator_id+'" class="font18 colorA">'+comments[n].creator_name+' </a>'
                    str +='<span class="font14 colorF"> 执业'+comments[n].creator_practice_period+' </span>'
                    str +='<div class="fr operate">'
                      if(comments[n].is_liked){
                      str +='<a href="javascript:void(0);" class="favour-on" data-id="'+comments[n].id+'" title="已赞"><i></i><span class="font14 fl num">'+comments[n].like_count+'</span></a>'
                      }else{
                      str +='<a href="javascript:void(0);" class="favour-off" data-id="'+comments[n].id+'"  title="点赞"><i></i><span class="font14 fl num">'+comments[n].like_count+'</span></a>'
                      }
                      str +='<a href="javascript:void(0);" class="reward-on" data-id="'+comments[n].creator_id+'" data-href="'+comments[n].creator_name+'" data-avatar="'+comments[n].creator_avatar+'"><i></i><span class="font14 fl">打赏</span></a>'
                    str +='</div>'
                  str +='</div>'
                  str +='<div class="content1">'+comments[n].content+'</div>'
                  str +='<div class="font14 colorD">'+comments[n].difference_date+'</div>'
                str +='</div>'
            str +='</div>'
            str +='</li>'
          }

        }else{
          str ='<li class="textac"><span class="noData3"></span><p class="noDatafont">问题暂时还没得到回答<br>试试一对一咨询吧，更快得到答复</p></li>';
        }
          $(".lv-reply-list").append(str);
     

      	$(".lvimg").click(function(){
      		$(".lv-info").css("display","none");
      		$(this).parent().parent().siblings(".lv-info").css("display","block");
      	});
        // 头像下面的弹出款
      	$(document).mouseup(function(e){
      	  var region = $('.lvimg,.lv-info');   // 设置目标区域
      	  if(!region.is(e.target) && region.has(e.target).length == 0){
      	  	$(".lv-info").css("display","none");
      	  }
      	});
      	// 发布回答输入款边款变色
      	$(".left-view-box .textarea").focus(function(){  // 获得焦点
      		$(".left-view-box .textarea").css("border","1px solid #48b4ff")
      		$(".left-view-box .iconleft1").addClass("iconleft2").removeClass("iconleft1");
      	}).blur(function(){     // 失去焦点
      		$(".left-view-box .textarea").css("border","1px solid #e8e8e8");
      		$(".left-view-box .iconleft2").addClass("iconleft1").removeClass("iconleft2");
      	});
      	//弹出打赏
      	$(".reward-on").click(function(){
          if(islogin == "" || islogin == null){
            layer.msg("请登录后再打赏！");
            return false;
          }else if(!login_isLawyer_id == "" || !login_isLawyer_id == null){
              layer.msg("律师不能打赏");
              return false;
          }else{
            $(".Popup1 .lv-avatar").attr("src",$(this).attr("data-avatar"));
            $(".Popup1 .lv-realname").text($(this).attr("data-href"));
            var lawyer_id = $(this).attr("data-id");
            $(".to-reward").attr("data-id",lawyer_id);
        		affirm();
        		$(".layui-layer").css("border-radius","10px")
        		$(".layui-layer-setwin .layui-layer-close2").css({"right":"-40px","top":"0px"});
        		$(".layui-layer-ico").css("background","url("+jumpUrl+"/img/question2/iconlayer.png)");
          }
          // 去打赏
          $(".to-reward").click(function(){
              var money = $(".reward-money .money-num").text();
              var lawyer_id = $(".to-reward").attr("data-id");
              var to_reward_url = jumpUrl+"/order/reward"
              $.post(to_reward_url,{"money":money,"lawyer_id":lawyer_id},function(data){
                },'json').error(function(data){
                   var error =  $.parseJSON(data.responseText);
                }).success(function(data){
                  localStorage.setItem("view_url", JSON.stringify(location.href));
                  var id = data.data.order_id;
                  var loading = layer.msg('打赏订单生成，即将进入支付页面', {icon: 16},2000);
                  //window.location.href = jumpUrl+"/question/public/reward/"+id+'?'+timestamps;
                   window.location.href = jumpUrl+"/center/question_order/"+id+"/to_pay?type=1&"+timestamps;
                });
          })

      	});
        function affirm(){
          var Popup04 = $(".Popup1");
              layer.open({
                type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
                title: false, //标题
                closeBtn: true,  //关闭按钮
                shade:0.3,  //遮罩默认是0.3透明度
                area: ['355px', '413px'],
                skin: 'yourclass', //样式类名
                content: Popup04 //内容 
            });
        };
        
        $(".reward-money i").click(function(){
          var math_num = Math.round(Math.random()*8)+1;
          $(".money-num").text(math_num);

          });
          //点赞
          $(document).on('click', '.favour-off', function(e) {
            if(islogin == "" || islogin == null){
              layer.msg("请登陆后再点赞！"); 
            }else{
              var loading = layer.msg('操作进行中...', {icon: 16});
              var item_id = $(this).attr("data-id");
              var token = $("input:[name='_token']").val();
              var params = {};
              var $this = $(this);
              params['item_type'] = 'comment';
              params['item_id'] = item_id;
              params['_token'] = token;
              // console.info(params)
              var url = jumpUrl+'/like';
              $.post(url,params,function(data){
                }, "json").error(function(data) {
                  var error =  $.parseJSON(data.responseText);
                  // layer.msg(error.error_messages.item_type[0]);
                }).success(function(data) { 
                  $this.children(".num").text(data.data['likes_count']);
                  $this.addClass("favour-on").removeClass("favour-off");
                  $this.attr("title","已赞");
                  layer.msg('点赞成功');
                });
            }
            
            stopDefault(e);
          });
          //取消点赞
          $(document).on('click', '.favour-on', function(e) {
             layer.msg("您已经点过赞了")
          });



          //收藏

          $(document).on("click", ".collect-off", function(e){
            if(islogin == "" || islogin == null){
              layer.msg("请登录后再收藏！",{time:2000},function(){
                      $(".loginPopup").click();
                    });
            return false;
          }else{
            var loading = layer.msg('操作进行中...', {icon: 16});
            var $this = $(this);
            var url = jumpUrl+"/collect";
            var thisId = $this.attr("data-id");
            var _token = $("input[name='_token']").val();
            $.post(url,{'collect_type':'1', 'item_id':thisId, '_token':_token},function(){
              
            }, 'json').error(function(data){
                  var obj = $.parseJSON(data.responseText);
                  $.each(obj.error_messages, function(i, item){
                    layer.msg(item[0]);
                  });
            }).success(function(data){
              var obj = data.data;
                layer.msg('收藏成功');
                $(".collect-off").addClass("collect-on").removeClass("collect-off");
                $this.children('.shu').text(obj);
            });
            }
          });

          //取消收藏
          $(document).on("click", ".collect-on", function(e){
            if(islogin == "" || islogin == null){
            layer.msg("请登录后再收藏！");
            return false;
          }else{
            var loading = layer.msg('操作进行中...', {icon: 16});
            var $this = $(this);
            var url = jumpUrl+"/collect/delete";
            var thisId = $this.attr("data-id");
            var _token = $("input[name='_token']").val();
            $.post(url,{'collect_type':'1', 'item_id':thisId, '_token':_token},function(){
              
            }, 'json').error(function(data){
                  var obj = $.parseJSON(data.responseText);
                  $.each(obj.error_messages, function(i, item){
                    layer.msg(item[0]);
                  });
            }).success(function(data){
              var obj = data.data;
                layer.msg('取消收藏成功');
                $(".collect-on").addClass("collect-off").removeClass("collect-on");
            });
            }
          });

          //点击关注
        $(document).on('click', '.follow-off', function(e) {
          
          if(islogin == "" || islogin == null){
            layer.msg("请登录后再关注！");
            return false;
          }else{
            var loading = layer.msg('操作进行中...', {icon: 16});
            var $this = $(this);
            var follow_id = $(this).attr("data-id");
            var params = {};
            params['follow_id'] = follow_id;
            params['user_id'] = user_id;
            var url = jumpUrl+'/follow';
            $.post(url,params,function(data){
              }, "json").error(function(data) {
                var error =  $.parseJSON(data.responseText);
                $.each(error.error_messages, function(i, item){
                  layer.msg(item[0]);
                });

              }).success(function(data) {
                var obj = data.data;
                layer.msg(obj);
                $this.text("取消关注");
                $this.addClass("follow-on").removeClass("follow-off");
                })
            }
            stopDefault(e);
          });
         //取消关注
        $(document).on('click', '.follow-on', function(e) {
          if(islogin == "" || islogin == null){
            layer.msg("请登录后再关注！");
            return false;
          }else{
            var loading = layer.msg('操作进行中...', {icon: 16});
            var $this = $(this);
            var follow_id = $(this).attr("data-id");
            var params = {};
            params['follow_id'] = follow_id;
            params['user_id'] = user_id;
            var url = jumpUrl+'/follow/remove';
            $.post(url,params,function(data){
              }, "json").error(function(data) {
                var error =  $.parseJSON(data.responseText);
                $.each(error.error_messages, function(i, item){
                  layer.msg(item[0]);
                });

              }).success(function(data) {
                var obj = data.data;
                layer.msg(obj);
                $this.text("关  注");
                $this.addClass("follow-off").removeClass("follow-on");
                })
            }
            stopDefault(e);
          });

        // 发布回答
        $(".Release-response").click(function(){

          if(islogin == "" || islogin == null){
            layer.msg("请登录后再回答！");
            return false;
          }else if(login_isLawyer_id == "" || login_isLawyer_id == null){
            layer.msg("只有律师才能回答");
            return false;
          }else{
            var comment = $(".textarea").val();
            if (comment<1) {
              layer.msg("回答内容不能为空");
              return false;
            }else if (comment>1000) {
              layer.msg("回答内容不能超过1000个字");
              return false;
            }else{
              var loading = layer.msg('操作进行中...', {icon: 16});
              var response_url = jumpUrl+"/question/"+id+"/comment";
              $.post(response_url,{"comment":comment,"creator_id":user_id},function(data){
                },"json").error(function(data){
                  var error =  $.parseJSON(data.responseText);
                  $.each(error.error_messages, function(i, item){
                    layer.msg(item[0]);
                  });
                }).success(function(data){
                   var obj = data.data;
                   layer.msg("回答成功");
                   $(".lv-reply-list li").remove();
                   $(".textarea").val("");
                   get_lv_reply();

              });
            }
          }

        });

    });

  }

})
